<?php

/** 
 * @author Administrator
 * 
 * 
 */
require_once ('db_base.php');
class db_payment extends db_base
{
	private $payid = 0;

	function __construct($pid) {
		global $_SGLOBAL;
		$this->payid = $pid;
		parent::__construct($_SGLOBAL['db']);
	}
	
	public function create_order($uid)
	{
		global $_SGLOBAL;
		$setarr = array('uid'=>$uid,'pid'=>$this->payid,'addtime'=>$_SGLOBAL['timestamp']);
		return str_pad($this->inserttable("payment_order",$setarr,1),8,'0',STR_PAD_LEFT);
	}
	
	public function set_orderstat($id,$stat,$date='')
	{
		$id = intval($id);
		$this->db->query("update ".tname("payment_order")." set stat='".$stat."'".(empty($date)?'':" successtime=$date")." where id=".$id);
	}
	
	public function  add_points($uid,$amount,$buypoints,$rate)
	{
		if($this->db->fetch_array($this->db->query("select uid from ".tname('user_points')." where uid=$uid")))
		{
			$sql = "update ".tname('user_points')." set points = points+$buypoints";
			$sql .=	",aveprice=(points*aveprice+".$amount*$rate.")/(points+$buypoints),totalprice=totalprice+$amount where uid=$uid";
			$this->db->query($sql);
		}
		else {
			$this->inserttable('user_points',array('points'=>$buypoints,'aveprice'=>($amount*$rate)/$buypoints,'totalprice'=>$amount,'uid'=>$uid));
		}
	}
}

?>